<?php
/**
 * 
 * @authors 丛玉坤(you@example.org)
 * @date    2016-06-25 22:00:22
 * @version 一版
 */
namespace Home\Controller;
use Think\Controller;
class MemberController extends Controller {
     public function _initialize(){
       if(empty($_SESSION["id"])){
            echo "<script>
                    alert('您还未登陆！登录后重试');
                    location.href='/login/login';
                  </script>";
      }
    } 

    public function index()
    {  
         
    	$UID=$_SESSION['id'];
    	if (!empty($UID)) {
    		//用户基本信息
    		$User=M("User");
    		$Uarr=$User->field("id,tel,username,tsrc")->where('id='.$UID)->find();    
        //       if ($Uarr['tel']=='') {
        //       echo "<script>
        //               alert('您未进行手机认证，请认证手机号码');
        //               history.go(-1);
        //             </script>";
        //           $this->redirect('/Member/reg/user/'.$UID,'您未进行手机认证，请认证手机号码');
        // }  
                 
        // if($Uarr['openid']==''){
        //   $this->redirect('__ROOT__/Weixin/Index/getBaseInfo');
        // }else{
        //       if ($Uarr['tel']=='') {
        //       echo "<script>
        //               alert('您未进行手机认证，请认证手机号码');
        //             </script>";
        //           $this->redirect('/Member/reg/user/'.$UID,'您未进行手机认证，请认证手机号码');
        //      }
        //   }
        
    		// 用户钱包
    		$Qian=M("Qianbao");
    		$Qarr=$Qian->field('money')->where('uid='.$Uarr['id'])->find();
    		$this->Uarr=$Uarr;
    		$this->Qarr=$Qarr;
    		   
    	}
       $this->display();
    }
    public function MemberInfo()
    {

           $id=$_SESSION['id'];
		//用户基本信息
    		$User=M("User");
    		$Uarr=$User->field("id,username,sex,tel,userid,tsrc")->where('id='.$id)->find();
    		$this->Uarr=$Uarr; 
    	    $this->display();
    }
    //实名认证
    public function UserCard()
    {
    	$id=$_SESSION['id'];
    	if (IS_POST) {
    		$User=M("User");
    		 $data['realname']=I("post.realname");
    		 $data['userid']=I("post.idcard");
    		 $list=$User->where('id='.$id)->save($data);
             if ($list) {
                  echo "<script>
                          alert('认证成功');
                          history.go(-2);
                        </script>";

             }else{
                    echo "<script>
                          alert('认证失败');
                          history.go(-2);
                        </script>";
             }
    		   
    	}else{
        $this->display();
    	}
    }
    // 修改昵称
    public function upname()
    {  
      $id=$_SESSION['id'];
    	if (IS_POST) {
    		$User=M("User");
    		 $data['username']=I("post.user");
    		 $list=$User->where('id='.$id)->save($data);
             if ($list) {

                  echo "<script>
                          alert('修改成功');
                          history.go(-2);
                        </script>";

             }else{
                    echo "<script>
                          alert('未作任何修改');
                          history.go(-2);
                        </script>";
             }
    		   
    	}else{

        $this->display();
    	}
    }
    // 性别
    public function sex()
    {
    $User=M("User");
    $uid=$_SESSION['id'];
    $userinfo=$User->where('id='.$uid)->find();
    $this->userinfo=$userinfo;
    if (IS_POST) {
            $data['sex']=I("post.sex"); 
             $list=$User->where('id='.$uid)->save($data);
             if ($list) {
                  echo "<script>
                          alert('修改成功');
                          history.go(-2);
                        </script>";
             }else{
                    echo "<script>
                          alert('未作任何修改');
                          history.go(-2);
                        </script>";
             }
          }else{    
     
        $this->display();
    }     
    }
    // 会员
    public function vip()
    {
        $id=$_SESSION['id'];
       //用户基本信息
        $User=M("User");
        $Uarr=$User->field("id,tel,username,tsrc")->where('id='.$id)->find(); 
        // 判断是否是vip  
        $VIP=M('Vip');
        $VipEnd=$VIP->where('uid='.$id)->find();
		  $NowTime=strtotime($VipEnd['endtime'])-strtotime(time());
		if ($NowTime>0) {
      $this->Uarr=$Uarr;
      $this->VIP=$VipEnd;
      $this->assign('endtime',$VipEnd['endtime']);
      $this->display('xfvip');
		}else{
      // $data['vstus']=0;
      // $Vip->where('uid='.$id)->save($data);
       $this->display();
		}
        $this->Uarr=$Uarr;
       
    }




//vip续费
    public function xfvip()
    {
      
    }
 //会员信息
    public function members_recharge()
    {
       $uid=$_SESSION['id'];
       $q=I("get.q");
        if ($q) {
            $this->display();
        }else{ 
            $Qian=M("Qianbao");
            $Qm=$Qian->field('money')->where('uid='.$uid)->find();   
            $this->Qm=$Qm;     
            $time=I("post.time");
            $yuan=I("post.yuan");
           $sum=  $yuan * $time;
           $this->time=$time;
           $this->uid=$uid;
           $this->assign('sum',$sum);
           $this->display('vip_pay');
          
        }
    }
    //钱包
    public function mymoney()
    {
 	  $d=$_SESSION['id'];
        if (!empty($d)) {
           $User=M("User");
           $Uarr=$User->field("id,tel,username,tsrc")->where('id='.$d)->find();
           $this->tsrc=$Uarr['tsrc']; 
           $Qian=M("Qianbao");
           $Money=$Qian->field('id,money,uid')->where('uid='.$d)->find();
           $this->Money=$Money;
              
        }
      $this->display();  
    }
    //付款页面
    // public function pay()
    // {
    //       $uid=$_SESSION['id'];
    //     if ($uid) {
    //         $this->i=$uid;
    //         $this->display();
    //     }else{   
    //         echo "<pre>";
    //         print_r ($_POST);
    //         echo "</pre>";   
                                   
    //        $uid=I("post.u"); 
    //     }
      
    // }
    // 验证是否有支付密码、
    public function yanZheng()
    {
        $uid=$_SESSION['id'];
 
        if (IS_POST) {
               $User=M("Qianbao");
               $zf=$User->field('zfpassword')->where('uid='.$uid)->find();
              if ($zf['zfpassword']!='') {
                   echo '1';
               }else{
                echo "您未设置支付密码";
               }
                  
            }
                 
    }
    // 验证支付密码、
    public function check_pass()
    {
         $uid=$_SESSION['id'];
         $password=trim(I("post.pa"));
         $sum=intval(trim(I("post.sum")));
         $time_long=I("post.time");
        if ($uid!='') {
           $bao=M("Qianbao");
           $pass=$bao->where('uid='.$uid)->find();
           // 密码正确
            if ($pass['zfpassword']==$password) {
             $data['money']=$pass['money']-$sum;//扣除钱
             if ($data['money']<0) {
               echo "余额不足";
               die;
             }
              $list= $bao->where('uid='.$uid)->save($data);//更新数据
              if ($list!=false) {
                   // 会员信息
                    $vip=M("Vip");
                    $v['starttime']=date("Y-m-d H:i:s",time());//开始时间
                    $endtime = strtotime("+$time_long months",time());//结束时间
                    $v['endtime']=date("Y-m-d H:i:s",$endtime);  
                    $outtime = strtotime("+$time_long+3 months",time());//过期时间
                    $v['outtime']=date("Y-m-d H:i:s",$outtime); 
                    $v['vstus']=1;
                    $varr=$vip->where("uid=".$uid)->find();    
                        if (empty($varr['uid'])) {
                            $v['uid']=$uid;
                            $vi=$vip->add($v);  
                        }else{
                          $new=$vip->field("endtime")->where('uid='.$uid)->find();
                          $time=$new['endtime'];
                       
                       $long=strtotime($time)+30*24*3600;
                       $v['endtime']=date("Y-m-d H:i:s",$long);    
                            $vi=$vip->where("uid=".$uid)->save($v);//更新信息
                                if ($vi) {  
                                 //写入消费记录
                                  $jilu=M("Jilu");
                                  $datas['uid']=$uid;
                                  $datas['jine']=$sum;
                                  $datas['liuxiang']=1;
                                  $datas['yue']=$data['money'];
                                  $datas['jiao_type']=0;
                                  $datas['create_time']=date("Y-m-d H:i:s",time());
                                  $ji=$jilu->add($datas);
                            if ($ji) {

                               echo "1";
                               die;
                              }  else{
                                echo "0";
                                die;
                              }
                            $this-> yqjl();
                         }
                        }
              }
            
            }else{
                echo '密码错误';
            }

        }
    }


function yqjl()
{
 
                    //邀请奖励
                  $Jy=M("jilu");
                  $wheres['uid']=$_SESSION['id'];
                  $wheres['liuxiang']=1;
                  //充值金额
                  $k=$Jy->field("jine")->where($wheres)->order('create_time DESC')->select();
                  $yongjin=$k[0]['jine'];
                  $User=M("User");      
                  $userinfo=$User->field('yid,id,username')->where('id='.$uid)->find();
                  // 邀请实例化
                      $Y=D("Yaoqing");
                      $ydata['uid']=$uid;
                      $ydata['yid']=$userinfo['yid'];
                    if ($userinfo['yid']!='') {//上一级
                           $use=$User->where('id='.$userinfo['yid'])->find();
                           $ydata['jine']=$yongjin*0.15;
                           $ydata['username']=$userinfo['username'];
                           // $ydata['create_time']=date("Y-m-d H:i:s",time());  
                           $ydata['create_time']=$Y->getdate();
                           $yao=$Y->add($ydata);
                           if ($yao) {
                              $jdata['uid']=$userinfo['yid'];
                              $jdata['jine']=$ydata['jine'];
                              $jdata['liuxiang']=3;
                              $yue=$bao->field("money")->where('uid='.$userinfo['yid'])->find();
                              $jdata['yue']=$yue['money']+$ydata['jine'];
                              $jdata['jiao_type']=1;
                              $jdata['create_time']=$Y->getdate();
                              $jj=$Jy->add($jdata);
                              //写入钱包
                             $yi=$bao->field('money,uid')->where('uid='.$userinfo['yid'])->find();
                             $yq['money']=$yi['money']+$yongjin*0.15;
                             $jiang= $bao->where('uid='.$yi['uid'])->save($yq);
                           }
                      if ($use['yid']!='') {//上二级
                         $us=$User->field('yid,id,username')->where('id='.$use['yid'])->find();//上一级信息
                            $ydata['uid']=$use['id'];
                            $ydata['yid']=$us['id'];
                            $ydata['username']=$use['username'];       
                            $ydata['jine']=$yongjin*0.10;                               
                            $yao1=$Y->add($ydata);
                            //写入钱包
                             $yi1=$bao->field('money,uid')->where('uid='.$us['id'])->find();
                             $yq1['money']=$yi1['money']+$yongjin*0.10;
                             $jiang1= $bao->where('uid='.$yi1['uid'])->save($yq1);
                            if ($yao1) {
                             $jta['uid']=$us['id'];
                              $jta['jine']=$ydata['jine'];
                              $jta['liuxiang']=3;
                              $yue2=$bao->field("money")->where('uid='.$use['id'])->find();
                              $jta['yue']=$yue2['money']+$ydata['jine'];
                              $jta['jiao_type']=1;
                              $jta['create_time']=$Y->getdate();
                              $jj1=$Jy->add($jta);
                           }
                        if ($us['yid']!='') {//上三级
                          $u=$User->where('id='.$us['yid'])->find();
                          $ydata['uid']=$us['id'];
                          $ydata['yid']=$u['id'];
                          $ydata['jine']=$yongjin*0.05;
                          $ydata['username']=$us['username'];
                          $yao2=$Y->add($ydata);
                            if ($yao2) {
                              $yue3=$bao->field("money")->where('uid='.$u['id'])->find();
                              $ta['uid']=$u['id'];
                              $ta['jine']=$ydata['jine'];
                              $ta['liuxiang']=3;
                              $ta['yue']=$yue3['money']+$ydata['jine'];
                              $ta['jiao_type']=1;
                              $ta['create_time']=$Y->getdate();
                              $jj2=$Jy->add($ta);
                             //写入钱包
                               $yi2=$bao->field('money,uid')->where('uid='.$u['id'])->find();
                               $yq2['money']=$yi2['money']+$yongjin*0.05;
                               $jiang1= $bao->where('uid='.$yi2['uid'])->save($yq2);
                           }                            
                        }
                      }
                    }
                }






      //短信
    public function reg()
    {
    $UID=$_SESSION['id'];
      if ($UID=='') {
           $UID=I("post.id");
       }
     $User=M("User");
     $Uarr=$User->field("id,tel,username,tsrc")->where('id='.$UID)->find();
     $this->Uarr=$Uarr;
if($_POST){

$tel=$_POST['mobile'];
$User=M("User");
$uarr=$User->field('tel')->where('tel='.$tel)->find();
    if ($uarr['tel']!='') {
          echo "<script>
                  alert('该电话已注册，请更换后重试');
                  history.go(-1);
                </script>";
                die;
    }

  //echo '<pre>';print_r($_POST);print_r($_SESSION);
  if($_POST['mobile']!=$_SESSION['mobile'] or $_POST['mobile_code']!=$_SESSION['mobile_code'] or empty($_POST['mobile']) or empty($_POST['mobile_code'])){
    exit('手机验证码输入错误。'); 
  }else{

    $_SESSION['mobile'] = '';
    $_SESSION['mobile_code'] = '';  

            $data['tel']=I('post.mobile');
            if (!empty($data)) {
              $u=$User->where('id='.$UID)->save($data);
              if ($u) {

                    echo "<script>
                            alert('认证成功。');
                            location.href='/Member/index';
                          </script>";
                die;
                // $this->redirect("");
              }else{
                $u->getError();
              }
            }
       
    
  }
}
  // function random($length = 6 , $numeric = 0) {
  //   PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
  //   if($numeric) {
  //     $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
  //   } else {
  //     $hash = '';
  //     $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
  //     $max = strlen($chars) - 1;
  //     for($i = 0; $i < $length; $i++) {
  //       $hash .= $chars[mt_rand(0, $max)];
  //     }
  //   }
  //   return $hash;
  // }
  $_SESSION['send_code'] = random(6,1);

         $this->display();
      }

public function sms()
{
  header("Content-type:text/html; charset=UTF-8");

// function Post($curlPost,$url){
//     $curl = curl_init();
//     curl_setopt($curl, CURLOPT_URL, $url);
//     curl_setopt($curl, CURLOPT_HEADER, false);
//     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//     curl_setopt($curl, CURLOPT_NOBODY, true);
//     curl_setopt($curl, CURLOPT_POST, true);
//     curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
//     $return_str = curl_exec($curl);
//     curl_close($curl);
//     return $return_str;
// }
// function xml_to_array($xml){
//   $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
//   if(preg_match_all($reg, $xml, $matches)){
//     $count = count($matches[0]);
//     for($i = 0; $i < $count; $i++){
//     $subxml= $matches[2][$i];
//     $key = $matches[1][$i];
//       if(preg_match( $reg, $subxml )){
//         $arr[$key] = xml_to_array( $subxml );
//       }else{
//         $arr[$key] = $subxml;
//       }
//     }
//   }
//   return $arr;
// }
// function random($length = 6 , $numeric = 0) {
//   PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
//   if($numeric) {
//     $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
//   } else {
//     $hash = '';
//     $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
//     $max = strlen($chars) - 1;
//     for($i = 0; $i < $length; $i++) {
//       $hash .= $chars[mt_rand(0, $max)];
//     }
//   }
//   return $hash;
// }
$target = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";

$mobile = $_POST['mobile'];
$send_code = $_POST['send_code'];

$mobile_code = random(4,1);
if(empty($mobile)){
  exit('手机号码不能为空');
}

if(empty($_SESSION['send_code']) or $send_code!=$_SESSION['send_code']){
  //防用户恶意请求
  exit('请求超时，请刷新页面后重试');
}

$post_data = "account=cf_yingteng&password=1234567&mobile=".$mobile."&content=".rawurlencode("您的验证码是：".$mobile_code."。请不要把验证码泄露给其他人。");
//密码可以使用明文密码或使用32位MD5加密
$gets =  xml_to_array(Post($post_data, $target));
if($gets['SubmitResult']['code']==2){
  $_SESSION['mobile'] = $mobile;
  $_SESSION['mobile_code'] = $mobile_code;
}
echo $gets['SubmitResult']['msg'];
}



    //设置密码
    public function setPass()
    {   
           $Qian=M("Qianbao");
           $USER=$Qian->field('zfpassword')->where('uid='.$_SESSION['id'])->find();
           $this->display();

     
    }
    public function upPw()
    {     
    $pass=I("post.password");
    $pass=trim($pass);
    $data['zfpassword']=md5($pass);
    $USER=M("Qianbao");
       $U=$USER->where('uid='.$_SESSION['id'])->save($data);
    if ($U) {
          
          echo "<script>
                  alert('成功');
                  location.href='/Member/index';//客户端跳转
                </script>";
    }else{
      echo "<script>
                  alert('失败');
                  history.go(-1);
                </script>";
    }
    }
    //亲情管理
    public function qinyou()
    {

        $Q=M("Qinqing");    
        $Qarr=$Q->where('uid='.$_SESSION['id'])->select();    
        $this->Qarr=$Qarr;   
      $this->display();
    }
    //删除
    public function qindel()
    {
      $d=I("post.qid");
       if ($d!='') {
         $Bao=M('Baodan')->where('qid='.$d)->select();
         if (!empty($Bao)) {
          echo "0";
           die;
         }else{
            $qin=M("Qinqing");
            $list=$qin->where('id='.$d)->delete();
            if ($list) {
              echo 1;
              die;

            }else{
              echo 2;
              die;
            }
         }
       }
    }
    //新增亲情
    public function xinzeng()
    {
      $this->display();
    }
    public function qadd()
    {
      $q=D("Qinqing");
      if ($q->create()) {
        $list=$q->add();
       // echo $q->getLastSql();

        if ($list!=false) { 
              echo "<script>
                      alert('添加成功');
                location.href='/Member/qinyou';//客户端跳转
                    </script>";
        }else{
                  echo "<script>
                      alert('添加失败');
                      history.go(-1);
                    </script>";
        }
      }else{
        $this->error($q->getError());
      }
        
    }


      // 银行卡管理
    public function bankcard()
    {
      $user=$_SESSION['id'];
 
          $Bank=D("Bank");
          $Barr=$Bank->chaxun($user);
          $this->Barr=$Barr;
          $this->display();
     
    }

    public function addbank()
    {
      if (IS_POST) {
      $Bank=D("Bank");
      $Bank->addcard();
     }else{        
     $this->display();
  }
    }

    public function jie()
    {
      if (IS_POST) {
        $bid=I("post.id");
        $Bank=M("Bank");
        $list=$Bank->where('bid='.$bid)->delete();
        if ($list) {
          echo "1";
        }else{
          echo "0";
        }
      }
        
    }
    //绑定支付宝
    public function alipay()
    {

     $user=$_SESSION['id'];
     $ali=D("Alipay")->chaxun($user);
     $this->Alrr=$ali;    
      $this->display();
    }
        public function ajie()
    {
      if (IS_POST) {
        $aid=I("post.id");
        $Bank=M("Alipay");
        $list=$Bank->where('aid='.$aid)->delete();
        if ($list) {
          echo "1";
        }else{
          echo "0";
        }
      }
        
    }

    public function addalipay()
    {
      if (IS_POST) {
      $Bank=D("Alipay");
      $Bank->addcard();
     }else{        
     $this->display();
  }
    }
    // 交易记录
    public function deal_list()
    {
     
    $user=$_SESSION['id'];

       $JL=M("Jilu");
       $Jarr=$JL->where('uid='.$user)->select();
       $this->Jarr=$Jarr; 
        $this->display();
        
    }
    //记录详情
    public function deal_info($id)
    {
      if (isset($id)) {
         $J=M("Jilu");
         $Jarr=$J->where('lid='.$id)->find();
         $this->Jarr=$Jarr;
       }
       $this->display();
    }
    public function pay_guanli()
    {
      $this->display();
    }
    //提现
    public function money_tixian()
    {
      $this->display();
    }
    //邀请收益
    public function revenue()
    { 

    $user=$_SESSION['id'];

     $Yao=M("Yaoqing");
     $Yarr=$Yao->where('yid='.$user)->select();
      $arr=array();
      foreach ($Yarr as $key => $v) {
        array_push($arr,$v['jine']);
      }
      // 总金额
    $count=count($arr);
    $this->count=$count;
    $sum=array_sum($arr);
    $this->sum=$sum;        
     $this->Yarr=$Yarr;
     $this->display();
    }
}

